import 'package:demo/constants/route_location.dart';
import 'package:demo/core/router/app_router_delegate.dart';
import 'package:demo/widget/banner_swiper.dart';
import 'package:demo/widget/image/custom_image.dart';
import 'package:flutter/material.dart';

class GuidePage extends StatefulWidget {
  @override
  _GuideState createState() => _GuideState();
}

class _GuideState extends State<GuidePage> {
  /// assets引导页图片
  final List<AssetImage> _assetsBundlePicture = [
    AssetImage("assets/images/guide/guide_1.png"),
    AssetImage("assets/images/guide/guide_2.png"),
    AssetImage("assets/images/guide/guide_3.png"),
  ];

  @override
  void didChangeDependencies() {
    // 预先加载的图片需要手动去释放
    _assetsBundlePicture.forEach((AssetImage s) => precacheImage(s, context));
    super.didChangeDependencies();
  }

  @override
  void deactivate() {
    _assetsBundlePicture.forEach((element) => element.evict());
    super.deactivate();
  }

  @override
  void dispose() {
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: SafeArea(
        top: true,
        child: Stack(
          children: [
            BannerSwiper(
              bannerLength: _assetsBundlePicture.length,
              autoPlay: false,
              loop: false,
              itemBuilder: (BuildContext context, int index) {
                return CustomImage.asset(
                  _assetsBundlePicture[index].assetName,
                  fit: BoxFit.fill,
                );
              },
            ),
            Positioned(
              top: 12,
              right: 12,
              child: GestureDetector(
                onTap: () {
                  AppRouterDelegate.of().setRoot(EXAMPLE_LIST);
                },
                child: Container(
                  width: 44,
                  height: 24,
                  alignment: Alignment.center,
                  decoration: BoxDecoration(
                    color: Colors.white,
                    borderRadius: BorderRadius.circular(22.5),
                  ),
                  child: const Text(
                    "跳过",
                    style: TextStyle(fontSize: 12.0, color: Colors.black),
                  ),
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }
}
